<?php
$outputpath="/mnt/default/v.madv360.com/php/uploads/files/";
require_once '/mnt/default/crontabscript/examples/EasyMySQLi.inc.php';
$db = new EasyMySQLi('localhost', 'root', 'root', 'madv_db1');
$tasklist=array();
$j=0;
try {
  //$result=$db->queryAllRows('select filename,url from file WHERE (appconfirm=1 or serverconfirm=1) and type=1 and status = 0 limit 1');
  $result=$db->queryAllRows('select filename,url from file WHERE type=1 and status = 0 limit 1');
  if(!empty($result)){
    for($i=0;$i<count($result);$i++){
      if(!empty($result[$i]["filename"])){
        exec(escapeshellcmd("/usr/local/bin/ffmpeg -i ".escapeshellarg($result[$i]["url"]).escapeshellarg($result[$i]["filename"]).".mp4 -y -f image2 -t 0.001 ".escapeshellarg($outputpath.$result[$i]["filename"])));
        if(is_file($outputpath.$result[$i]["filename"])){
          $originalfps=exec('/usr/local/bin/ffmpeg -i '.escapeshellarg($result[$i]["url"]).escapeshellarg($result[$i]["filename"]).'.mp4 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p"');
          $img_info=getimagesize($outputpath.$result[$i]["filename"]);
          if ($img_info[1]>=720){
            if($originalfps<30){
              save($j++,$result[$i]["filename"],$result[$i]["url"],30,"1440x720","2000000","libx264","copy",$outputpath,0);
              save($j++,$result[$i]["filename"],$result[$i]["url"],30,"1440x720","2000000","libx265","copy",$outputpath,0);
            }else{
              save($j++,$result[$i]["filename"],$result[$i]["url"],60,"1440x720","2500000","libx264","copy",$outputpath,0);
              save($j++,$result[$i]["filename"],$result[$i]["url"],60,"1440x720","2500000","libx265","copy",$outputpath,0);
            }
            if ($img_info[1]>=1080){
              if($originalfps<30){
                save($j++,$result[$i]["filename"],$result[$i]["url"],30,"2304x1152","5000000","libx264","copy",$outputpath,0);
                save($j++,$result[$i]["filename"],$result[$i]["url"],30,"2304x1152","5000000","libx265","copy",$outputpath,0);
              }else{
                save($j++,$result[$i]["filename"],$result[$i]["url"],60,"2304x1152","6250000","libx264","copy",$outputpath,0);
                save($j++,$result[$i]["filename"],$result[$i]["url"],60,"2304x1152","6250000","libx265","copy",$outputpath,0);
              }
              if ($img_info[1]>=1728){
                save($j++,$result[$i]["filename"],$result[$i]["url"],30,"3456x1728","12000000","libx264","copy",$outputpath,0);
                save($j++,$result[$i]["filename"],$result[$i]["url"],30,"3456x1728","12000000","libx265","copy",$outputpath,0);
              }
            }
            $db->queryNoResult('update file set status=1 where filename=?',$result[$i]["filename"]);//已经生成队列1
          }else{
            //用户自己上传的视频不够720p处理成720p
            //$db->queryNoResult('update file set status=-1 where filename=?',$result[$i]["filename"]);//分辨率太低-1
            save($j++,$result[$i]["filename"],$result[$i]["url"],30,"1440x720","2000000","libx264","copy",$outputpath,0);
            save($j++,$result[$i]["filename"],$result[$i]["url"],30,"1440x720","2000000","libx265","copy",$outputpath,0);
            $db->queryNoResult('update file set status=1 where filename=?',$result[$i]["filename"]);//已经生成队列1
          } 
        }else{
          $db->queryNoResult('update file set status=-2 where filename=?',$result[$i]["filename"]);//不是视频-2
        }
      }
    }
    //print_r($tasklist);
    for($i=0;$i<count($tasklist);$i++){
      if(!empty($tasklist[$i]["filename"])){
        //exec("wget -c ".$tasklist[$i]["url"].$tasklist[$i]["filename"].".mp4 -O ".$tasklist[$i]["outputpath"].$tasklist[$i]["filename"].".mp4");
        if (is_file($tasklist[$i]["outputpath"].$tasklist[$i]["filename"].".mp4")){
          //$result=$db->queryNoResult('INSERT INTO file_transcode (`filename`,`url`,`fps`,`resolution`,`bitrate`,`videocodec`,`audiocodec`,`outputpath`,`status`) VALUES (?,?,?,?,?,?,?,?,?)', $tasklist[$i]['filename'],$tasklist[$i]['url'],$tasklist[$i]['fps'],$tasklist[$i]['resolution'],$tasklist[$i]['bitrate'],$tasklist[$i]['videocodec'],$tasklist[$i]['audiocodec'],$tasklist[$i]['outputpath'],$tasklist[$i]['status']);
        }else{
          exec("wget -c ".$tasklist[$i]["url"].$tasklist[$i]["filename"].".mp4 -O ".$tasklist[$i]["outputpath"].$tasklist[$i]["filename"].".mp4");
        }
        $result=$db->queryNoResult('INSERT INTO file_transcode (`filename`,`url`,`fps`,`resolution`,`bitrate`,`videocodec`,`audiocodec`,`outputpath`,`status`) VALUES (?,?,?,?,?,?,?,?,?)', $tasklist[$i]['filename'],$tasklist[$i]['url'],$tasklist[$i]['fps'],$tasklist[$i]['resolution'],$tasklist[$i]['bitrate'],$tasklist[$i]['videocodec'],$tasklist[$i]['audiocodec'],$tasklist[$i]['outputpath'],$tasklist[$i]['status']);
      }
    }
  }else {
  }
}catch (MySQLiQueryException $ex) {
  //echo '{"rval":3}';// 失败
}
function save($i,$filename,$url,$fps,$resolution,$bitrate,$videocodec,$audiocodec,$outputpath,$status){
  global $tasklist;
  $tasklist[$i]["filename"]=$filename;
  $tasklist[$i]["url"]=$url;
  $tasklist[$i]["fps"]=$fps;
  $tasklist[$i]["resolution"]=$resolution;
  $tasklist[$i]["bitrate"]=$bitrate;
  $tasklist[$i]["videocodec"]=$videocodec;
  $tasklist[$i]["audiocodec"]=$audiocodec;
  $tasklist[$i]["outputpath"]=$outputpath;
  $tasklist[$i]["status"]=$status;
}
